<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import * as echarts from 'echarts'


  import resize from './mixins/resize'

  const animationDuration = 1000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')

        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: 10,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul','Aug','Aug'],
            axisTick: {
              alignWithLabel: true
            },
            axisLabel:{
              color: '#43425D',
              fontSize: 9,
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            },
            splitLine:{
              show: false
            },
            axisLine: {
              textStyle: {
                color: '#999'
              },

            },
            axisLabel: {
              color: '#43425D',
              fontSize: 7,
              formatter: function (value, index) {
                if (value >= 1000) {
                  value = value / 1000 + 'k';
                } else if (value < 1000) {
                  value = value;
                }
                if (value <= -1000) {
                  value = value / 1000 + 'k';
                } else if (value < 1000) {
                  value = value;
                }
                return value
              }
            }
          }],
          series: [{
            name: 'pageA',
            type: 'bar',
            stack: 'vistors',
            barWidth: '38px',
            itemStyle: {
              color: '#F1C40F',
              barBorderRadius:[4, 4 ,0, 0]
            },
            data: [5000, 5000, 5000, 5000, 5000, 5000, 5000,5000,5000],
            animationDuration
          },
            {
              name: 'pageB',
              type: 'bar',
              stack: 'vistors',
              barWidth: '38px',
              itemStyle: {
                color: '#EAF0F4',
                barBorderRadius:[4, 4 ,0, 0]
              },
              data: [-5000, -5000, -5000, -5000, -5000, -5000, -5000,-5000,-5000],
              animationDuration
            },]
        })
      }
    }
  }
</script>
